package com.yfp.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.common.core.domain.ResponseEnum;
import com.yfp.common.core.enums.PayStatusEnum;
import com.yfp.operation.domain.ClientCoupon;
import com.yfp.operation.domain.OrderComboDO;
import com.yfp.operation.domain.OrderListDO;
import com.yfp.operation.domain.dto.LengthenDateDTO;
import com.yfp.operation.enums.ClientCouponStatusEnums;
import com.yfp.operation.mapper.ClientCouponMapper;
import com.yfp.operation.service.ClientCouponService;
import com.yfp.operation.service.OrderComboService;
import com.yfp.operation.service.OrderListService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @description 针对表【client_coupon(客户优惠券)】的数据库操作Service实现
 * @createDate 2024-07-12 15:49:29
 */
@Slf4j
@Service
public class ClientCouponServiceImpl extends ServiceImpl<ClientCouponMapper, ClientCoupon> implements ClientCouponService {

    @Resource
    private ClientCouponMapper clientCouponMapper;
    @Resource
    private OrderListService orderListService;
    @Resource
    private OrderComboService orderComboService;

    /**
     * 后台退 优惠券
     *
     * @param orderListId
     * @return
     */
    @Override
    public void refund(String orderListId) {
        log.info("后台退优惠券orderListId:{}", orderListId);
        OrderListDO orderList = orderListService.getById(orderListId);
        List<OrderListDO> orderListDOS = orderListService.getByOrderSn(orderList.getOrderSn());
        OrderComboDO orderCombo = orderComboService.getById(orderList.getOrderSn());

        long count = orderListDOS.stream().filter(item -> {
            if (PayStatusEnum.REFUNDING.getCode().equals(item.getPayStatus())) {
                return false;
            }
            if (PayStatusEnum.REFUNDED.getCode().equals(item.getPayStatus())) {
                return false;
            }
            if (item.getId().equals(orderListId)) {
                return false;
            }
            return true;
        }).count();
        if (count == 0) {
            // 订单的 所有套餐对发起退款时才 退优惠券。部分退款时 不退优惠券
            // 修改优惠券
            // LambdaUpdateWrapper<OrderComboDO> orderComboUpdateWrapper = Wrappers.lambdaUpdate(OrderComboDO.class);
            // orderComboUpdateWrapper.eq(OrderComboDO::getOrderId, orderCombo.getOrderId());
            // orderComboUpdateWrapper.set(OrderComboDO::getClientCouponId, null);
            // orderComboUpdateWrapper.set(OrderComboDO::getCouponId, null);
            // orderComboService.update(orderComboUpdateWrapper);
            this.updateStatusById(orderCombo.getClientCouponId(), ClientCouponStatusEnums.NORMAL.getCode());
        }
    }

    /**
     * 修改优惠券状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        clientCouponMapper.updateStatusById(id, status);
    }

    @Override
    public void callUpdateClientCouponStatus() {
        clientCouponMapper.callUpdateClientCouponStatus();
    }

    @Override
    public void lengthen(LengthenDateDTO lengthenDateDTO) {
        List<ClientCoupon> clientCoupons = clientCouponMapper.selectBatchIds(lengthenDateDTO.getClientCouponIs());
        for (ClientCoupon clientCoupon : clientCoupons) {
            clientCoupon.setPostponeDay(lengthenDateDTO.getDayNumber());
            clientCouponMapper.updateById(clientCoupon);
        }
    }

    @Override
    public List<ClientCoupon> listByCouponPackIdAndClientIds(Long couponPackId, List<String> clientIds) {
        if (CollUtil.isEmpty(clientIds)) {
            return CollUtil.newArrayList();
        }
        return clientCouponMapper.selectByCouponPackIdAndClientIds(couponPackId, clientIds);
    }

    /**
     * 核销实体券
     * @param ccId 客户领取表id
     * @return
     */
    @Override
    public ResponseEnum verification(Long ccId) {
        ClientCoupon clientCoupon = clientCouponMapper.selectById(ccId);
        if (BeanUtil.isEmpty(clientCoupon)) {
            return ResponseEnum.COUPON_NOT_EXIST;
        } else if (clientCoupon.getStatus() != 1) {
            return ResponseEnum.COUPON_NOT_COUNT;
        }



        return null;
    }

    /**
     * 储值优惠券失效
     * @param cbId
     */
    @Override
    public void storedCouponExpire(Long cbId) {
        clientCouponMapper.storedCouponExpire(cbId);
    }

}




